package languageacquisition;

import java.util.Arrays;
import java.util.HashMap;

import javax.swing.JLabel;
import javax.swing.table.AbstractTableModel;

import languageacquisition.ontology.LanguageSymbol;
import languageacquisition.ontology.Property;

public class DictionaryTableModel extends AbstractTableModel {

	private static final long serialVersionUID = -4862067208422450714L;

	private HashMap<Property, HashMap<LanguageSymbol, Correlation>> dictionary;

	private Property[] properties = new Property[0];
	private LanguageSymbol[] symbols = new LanguageSymbol[0];

	public DictionaryTableModel(HashMap<Property, HashMap<LanguageSymbol, Correlation>> dictionary) {
		super();
		this.dictionary = dictionary;
		this.properties = dictionary.keySet().toArray(properties);
		Arrays.sort(properties);
		if (properties.length > 0) {
			symbols = dictionary.get(properties[0]).keySet().toArray(symbols);
			Arrays.sort(symbols);
		}
	}

	@Override
	public int getColumnCount() {
		return symbols.length + 1;
	}

	public HashMap<Property, HashMap<LanguageSymbol, Correlation>> getDictionary() {
		return dictionary;
	}

	@Override
	public int getRowCount() {
		return properties.length + 1;
	}

	@Override
	public Object getValueAt(int rowIndex, int colIndex) {
		if (rowIndex == 0) {
			return getColumnName(colIndex);
		} else
		
		if (colIndex == 0) {
			return properties[rowIndex-1];
		}
		Correlation corr = dictionary.get(properties[rowIndex-1]).get(symbols[colIndex-1]);
		return corr;
	}

	public void setDictionary(HashMap<Property, HashMap<LanguageSymbol, Correlation>> dictionary) {
		this.dictionary = dictionary;
	}

	@Override
	public String getColumnName(int colIndex) {
		if (colIndex == 0) return "";
		return symbols[colIndex-1].toString();
	}

}
